#!/bin/bash
# $Header: /home/remik/osobiste/cvs_repo/scripto/oracle/replay_sh/replay.wrap,v 1.1 2012-05-07 13:49:15 remik Exp $
#
# !!!! D A N G E R !!!
#
# This script is very powerfull and as such very dangerous. 
# Use with utmost caution
#
# Used to issue the same shell commands statement on multiple hosts
# It is also assumed that I am able to connect to those hosts using priv-key
#
# The list of username#hostname pairs is stored in $HOST_LIST
# The commands to be executed are stored in $COMMAND_FILE
#
# Sample replay.commands
# $ cat replay.commands
# ps -ef | grep pmon; pwd; uname -a

# Sample replay.hostlist
# $ cat replay.hostlist
# oracle#192.168.1.159#EBSDB#uname -a
#
#

LOCKFILE=/tmp/replay.lock
TMP_FILE=/tmp/replay_sh.tmp

COMMAND_FILE=${HOME}/scripto/linux/replay_sh/replay.commands

HOST_LIST=${HOME}/scripto/linux/replay_sh/replay.hostlist


# Load usefull functions
if [ ! -f $HOME/scripto/bash/bash_library.sh ]; then
  echo "[error] $HOME/scripto/bash/bash_library.sh not found. Exiting. "
  exit 1
else
  . $HOME/scripto/bash/bash_library.sh
fi

#INFO_MODE=DEBUG
RECIPIENTS='remigiusz_boguszewicz'
check_lock $LOCKFILE
check_file $COMMAND_FILE
check_file $HOST_LIST


msgd "COMMAND_FILE: $COMMAND_FILE"
msgd "HOST_LIST: $HOST_LIST"

# Set lock file
touch $LOCKFILE

date
echo -n -e '\E[35m'
echo "The following command:"
echo -n -e '\E[33m'

COMMAND_FILE_CONTENT=`cat ${COMMAND_FILE}`
echo $COMMAND_FILE_CONTENT

echo -n -e '\E[35m'
echo "Is going to be executed on the following hosts:"
echo -n -e '\E[33m'
cat $HOST_LIST

echo -n -e '\E[35m'
read -p "Do you REALLY want to execute the above commands on databases in selected hosts (yes/no)? " REPLY
if [ ! "$REPLY" = "yes" ]; then 
  echo "User aborded mission." 
  rm -f $LOCKFILE
  exit 0
fi

# this is what I use if I need to provide the password
msgw "Provide the password:"
read -s PASSWORD


msgi "Logs from this replay: $LOG"
# Loop through HOST_LIST try to PING all entries
exec 3<> $HOST_LIST
while read line <&3
do {
  # filter out lines that are comments
  line=`echo $line | grep -v "^#" | grep -v "^--"`
  if [ -z "$line" ]; then
    continue
  fi
  
  OS_USER_NAME=`echo $line | awk -F"#" '{ print $1 }'`
  HOST_NAME=`echo $line | awk -F"#" '{ print $2 }'`
  LDAP_CN=`echo $line | awk -F"#" '{ print $3 }'`
  FIRST_CMD=`echo $line | awk -F"#" '{ print $4 }'`
  
  #echo $OS_USER_NAME $HOST_NAME
  #echo -e '\E[30m black \E[31mred \E[32mgreen \E[33myellow \E[34mblue \E[35mmagenta \E[36mcyan \E[37mwhite'
  #echo -e '\E[30m black'
  
  # Sanity check: for ping reachability
  msgd "HOST_NAME: $HOST_NAME"
  $PING -n 1 $HOST_NAME >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    msgd "System $OS_USER_NAME @ $HOST_NAME found (ping). Continuing $LDAP_CN."
    echo "------"
    echo "$LDAP_CN"
    echo "------"
  else
    msgi "[warning] Host $HOST_NAME not found (ping). Skipping $LDAP_CN."
    continue
  fi

  # this is what I use if I have private key stored on destination server
  # which enables password-less connection
  #echo $COMMAND_FILE_CONTENT | ssh -T -o BatchMode=yes -o ChallengeResponseAuthentication=no -o PasswordAuthentication=no -o PubkeyAuthentication=yes -o RSAAuthentication=no -2 -l $OS_USER_NAME $HOST_NAME

  # this is what I use if I need to provide the password
  ./ssh_expect.exp $OS_USER_NAME $HOST_NAME $PASSWORD "${FIRST_CMD}" "$COMMAND_FILE_CONTENT"
 
}  
done
exec 3>&-

# Upon exit, remove lockfile.
rm -f $LOCKFILE
rm -f ${TMP_FILE}

#msgi "Raw output"
#run_command "cat $LOG | grep --after-context=10000 'Logs from this replay:' | grep -v spawn | grep -v 'password:' | grep -v 'Raw output'"
